package com.timoon.webcomponent.utility.product.discount;

import org.apache.wicket.Page;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.timoon.app.resource.image.ImageLocation;
import com.timoon.sample.dao.model.Product;

/**
 *<p></p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */
public class Discount1Panel extends Panel {
	
	//通过这两个type指定两个商品
	public int DISCOUNT1_PRODUCT1_TYPE=85132447;
	public int DISCOUNT1_PRODUCT2_TYPE=85882447;
	
	@SpringBean(name="com.timoon.sample.service.DiscountServiceImpl")
	private Discount1Service service;
	
	private Class<? extends Page> targetPage;
	
	private String title;
	
	public Discount1Panel(String id,String title,Class<? extends Page> targetPage) {
		super(id);
		// TODO Auto-generated constructor stub
		
		
		this.title=title;
		
		this.targetPage=targetPage;
		
		renderPanel();
		
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		
		renderProduct1();
		
		renderProduct2();
		
		renderTitle();
	}

	private void renderTitle() {
		// TODO Auto-generated method stub
		add(new Label("wk_title",title));
	}

	private void renderProduct1() {
		// TODO Auto-generated method stub
		Product product=this.service.getProductByType(DISCOUNT1_PRODUCT1_TYPE);
		
		if(product==null){
			throw new RuntimeException("没有找到[type="+DISCOUNT1_PRODUCT1_TYPE+"]的product");
		}
		
		PageParameters pageParameters=new PageParameters();
		pageParameters.set("product_id", product.getId());
		
		BookmarkablePageLink link1=new BookmarkablePageLink<>("wk_link1",targetPage,pageParameters);
		add(link1);
		
		BookmarkablePageLink link2=new BookmarkablePageLink<>("wk_link2",targetPage,pageParameters);
		add(link2);
		
		link1.add(new Image("wk_img1",new PackageResourceReference(ImageLocation.class,"product/phone-small.png")));
		
		link2.add(new Label("wk_name1",product.getName()));
		
		add(new Label("wk_instruction1",product.getInstruction()));
		
	}

	private void renderProduct2() {
		// TODO Auto-generated method stub
		Product product=this.service.getProductByType(DISCOUNT1_PRODUCT2_TYPE);
		
		if(product==null){
			throw new RuntimeException("没有找到[type="+DISCOUNT1_PRODUCT2_TYPE+"]的product");
		}
		
		PageParameters pageParameters=new PageParameters();
		pageParameters.set("product_id", product.getId());
		
		BookmarkablePageLink link1=new BookmarkablePageLink<>("wk_link3",targetPage,pageParameters);
		add(link1);
		BookmarkablePageLink link2=new BookmarkablePageLink<>("wk_link4",targetPage,pageParameters);
		add(link2);
		
		link1.add(new Image("wk_img2",new PackageResourceReference(ImageLocation.class,"product/phone-small.png")));
		
		link2.add(new Label("wk_name2",product.getName()));
		
		add(new Label("wk_instruction2",product.getInstruction()));
	}

}
